/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package openjawmail;

import java.io.*;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.jar.*;

/**
 *
 * @author weifang
 */
public class updateJar {

    public static final int OPERATION_SUCCESSED=0;
    public static final int FILE_NOT_FOUND=1;
    public static final int FILE_PATH_INVALID=2;
    public static final int PATH_TYPE_FILE=3;
    public static final int PATH_TYPE_DIR=4;
    private static final String FILE_PATH_INVALID_EX="File path note valid or hasn't been setted up";
    private static final String UNKNOW_EX="Unknow error occured";
    private String jarToUpdate="";


    public InputStream readfile(String name_in) throws updateJarException
    {
            InputStream is=null;
            if(this.jarToUpdate.equals(""))
            {
                throw new updateJarException(updateJar.FILE_PATH_INVALID_EX);
            }else
            {
                try{
                    JarFile ajar=new JarFile(this.jarToUpdate);
                    Enumeration e=ajar.entries();
                    JarEntry entry=null;
                    while (e.hasMoreElements())
                    {
                        entry=(JarEntry)e.nextElement();
                        if (entry.getName().contains(name_in))
                        {
                               is=ajar.getInputStream(entry);
                        }
                    }
                }catch(Exception ee)
                {
                    OpenJawMailFunc.logError("readfile IO error: "+ee.getMessage());
                }finally
                {
                    return is;
                }
            }

    }

    public String getSelfJarPath(int Type)
    {
        try {
            if(Type==updateJar.PATH_TYPE_FILE)
            {
                return URLDecoder.decode(this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8");
            }else
            {
                String path=this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
                File tempFile=new File(path);
                return URLDecoder.decode(tempFile.getParent(),"UTF-8");
            }
        } catch (UnsupportedEncodingException ex) {
            return "";
        }

    }

    public int setJarToUpdate(String path_in)
    {
        if (!path_in.equals(""))
        {
            this.jarToUpdate=path_in;
            return updateJar.OPERATION_SUCCESSED;
        }else
        {
            return updateJar.FILE_PATH_INVALID;
        }
    }

    class updateJarException extends Exception
    {
        private String ex_message="";

        public updateJarException()
        {
            super();
            ex_message = updateJar.UNKNOW_EX;
        }

        public updateJarException(String message_in)
        {
            super(message_in);
            ex_message=message_in;
        }

        public String getExMessage()
        {
            return ex_message;
        }
    }
}
